package com.example.springboottools.temp;


public class CodeUtil {
	public static void main(String[] args) {
   System.out.println(brandCreateCodeNew("AA99"));
	}
	
	/**
	 * 品牌编码生成方法
	 * 范围 1000～ZZZZ
	 * @param code
	 * @return
	 */
	public static String brandCreateCode(String code){
		if(code!=null){
			if("ZZZZ".equals(code)){
				return null;
			}
			char[] chars=code.toCharArray();
			return String.valueOf(createCode(chars.length-1, chars));
		}
		return "1000";
	}
	public static String brandCreateCodeNew(String code){
		if(code!=null){
			if("ZZZZ".equals(code)){
				return null;
			}
			char[] chars=code.toCharArray();

			return String.valueOf(createNextCode(chars));
		}
		return "1000";
	}

	/**
	 * 颜色编码编码生成方法
	 * 范围 50000～ZZZZZ
	 * @param code
	 * @return
	 */
	public static String colorCreateCode(String code){
		if(code!=null){
			if("ZZZZZ".equals(code)){
				return null;
			}
			char[] chars=code.toCharArray();
			return String.valueOf(createCode(chars.length-1, chars));
		}
		return "50000";
	}
	
	/**
	 * 编码增长规则  old ZA99 ==》 new ZB00
	 * @param 
	 * @param chars
	 * @return
	 */
	public static char[] createCode(int i,char[] chars){
		if(48<=(int)chars[i] &&  (int)chars[i]<=57){  //数字处理
			if((int)chars[i]==57){  //判读当前字符是否是 9
				if(i>0){
					chars[i]='0';
					createCode(i-1,chars); //递归调用
				}else{
				    chars[i]='A';  
				    for(int num=1;num<chars.length;num++){
				    	chars[num]='0';
				    }
					return chars;
				}
			}else{
				chars[i]=(char)((int)chars[i]+1);
				return chars;
			}
		}else if(65<=(int)chars[i] && (int)chars[i]<=90 ){  //字母处理
			if((int)chars[i]==90){ //判读当前字符是否是 Z
				if(i>0){
					chars[i]='A'; //重置当前编码为A
					createCode(i-1,chars); //递归调用
				}else{
					chars[i]='Z';
					for(int num=1;num<chars.length;num++){
					   chars[num]='A';
					}
					return chars;
				}
			}else{
				chars[i]=(char)((int)chars[i]+1);
				return chars;
			}
		}
		return chars;
	}
	public static char[] createNextCode(char[] code) {
		createCode(code.length - 1, code);
		if (isAllNinesExceptFirstLetter(code)) {
			int firstNonLetterIndex = findFirstNonLetterIndex(code);
			if (firstNonLetterIndex >= 0) {
				if (code[firstNonLetterIndex] == '9') {
					code[firstNonLetterIndex] = 'A';
					for (int j = firstNonLetterIndex + 1; j < code.length; j++) {
						code[j] = '0';
					}
				} else {
					code[firstNonLetterIndex] = (char) ((int) code[firstNonLetterIndex] + 1);
					for (int j = firstNonLetterIndex + 1; j < code.length; j++) {
						code[j] = '0';
					}
				}
			}
		}
		return code;
	}

	private static int findFirstNonLetterIndex(char[] code) {
		for (int i = 0; i < code.length; i++) {
			if (48 <= (int) code[i] && (int) code[i] <= 57) {  // 数字
				return i;
			}
		}
		return -1;  // 没有找到非字母字符
	}

	private static boolean isAllNinesExceptFirstLetter(char[] code) {
		if (65 <= (int) code[0] && (int) code[0] <= 90) {  // 第一个字符是字母
			for (int i = 1; i < code.length; i++) {
				if (code[i] != '9') {
					return false;
				}
			}
			return true;
		}
		return false;
	}


	public static char[] createCodeNew(int i,char[] chars){
		if(48<=(int)chars[i] &&  (int)chars[i]<=57){  //数字处理
			if((int)chars[i]==57){  //判读当前字符是否是 9
				if(i>0){
					chars[i]='0';
					createCodeNew(i-1,chars); //递归调用
				}else{
				    chars[i]='A';
				    for(int num=1;num<chars.length;num++){
				    	chars[num]='0';
				    }
					return chars;
				}
			}else{
				chars[i]=(char)((int)chars[i]+1);
				return chars;
			}
		}else if(65<=(int)chars[i] && (int)chars[i]<=90 ){  //字母处理
			if((int)chars[i]==90){ //判读当前字符是否是 Z
				if(i>0){
					chars[i]='A'; //重置当前编码为A
					createCodeNew(i-1,chars); //递归调用
				}else{
					chars[i]='Z';
					for(int num=1;num<chars.length;num++){
					   chars[num]='A';
					}
					return chars;
				}
			}else{
				chars[i]=(char)((int)chars[i]+1);
				return chars;
			}
		}
		return chars;
	}

}
